using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 服务管理器 - 统一管理所有服务
/// </summary>
public class ServiceManager : MonoBehaviour
{
    private static ServiceManager m_Instance;
    public static ServiceManager Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = FindObjectOfType<ServiceManager>();
                if (m_Instance == null)
                {
                    GameObject go = new GameObject("ServiceManager");
                    m_Instance = go.AddComponent<ServiceManager>();
                }
            }
            return m_Instance;
        }
    }
    
    private Dictionary<System.Type, IService> m_Services = new Dictionary<System.Type, IService>();
    
    private void Awake()
    {
        if (m_Instance == null)
        {
            m_Instance = this;
            DontDestroyOnLoad(gameObject);
            InitializeServices();
        }
        else if (m_Instance != this)
        {
            Destroy(gameObject);
        }
    }
    
    /// <summary>
    /// 初始化所有服务
    /// </summary>
    private void InitializeServices()
    {
        // 注册核心服务
        RegisterService<GameConfigService>();
        RegisterService<EntityService>();
        RegisterService<SunSystem>();
        
        Logger.Log("ServiceManager", "All services initialized");
    }
    
    /// <summary>
    /// 注册服务
    /// </summary>
    public T RegisterService<T>() where T : class, IService, new()
    {
        var serviceType = typeof(T);
        
        if (m_Services.ContainsKey(serviceType))
        {
            return m_Services[serviceType] as T;
        }
        
        var service = new T();
        service.Initialize();
        m_Services[serviceType] = service;
        
        Logger.Log("ServiceManager", $"Service {serviceType.Name} registered");
        return service;
    }
    
    /// <summary>
    /// 获取服务
    /// </summary>
    public T GetService<T>() where T : class, IService
    {
        var serviceType = typeof(T);
        
        if (m_Services.TryGetValue(serviceType, out var service))
        {
            return service as T;
        }
        
        Logger.LogWarning("ServiceManager", $"Service {serviceType.Name} not found");
        return null;
    }
    
    /// <summary>
    /// 检查服务是否存在
    /// </summary>
    public bool HasService<T>() where T : class, IService
    {
        return m_Services.ContainsKey(typeof(T));
    }
    
    /// <summary>
    /// 注销服务
    /// </summary>
    public void UnregisterService<T>() where T : class, IService
    {
        var serviceType = typeof(T);
        
        if (m_Services.TryGetValue(serviceType, out var service))
        {
            service.Dispose();
            m_Services.Remove(serviceType);
            Logger.Log("ServiceManager", $"Service {serviceType.Name} unregistered");
        }
    }
    
    /// <summary>
    /// 获取所有服务
    /// </summary>
    public List<IService> GetAllServices()
    {
        return new List<IService>(m_Services.Values);
    }
    
    private void OnDestroy()
    {
        // 清理所有服务
        foreach (var service in m_Services.Values)
        {
            service.Dispose();
        }
        m_Services.Clear();
        
        if (m_Instance == this)
        {
            m_Instance = null;
        }
    }
}
